package com.shortlink.config;

import com.shortlink.common.interceptor.BlackListInterceptor;
import com.shortlink.common.interceptor.UserFlowRiskControlInterceptor;
import com.shortlink.common.interceptor.LoginInterceptor;
import com.shortlink.common.interceptor.RefreshTokenInterceptor;
import com.shortlink.common.properties.UserFlowRiskControlProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 添加拦截器
     * @param registry
     */
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    UserFlowRiskControlProperties userFlowRiskControlProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 登录拦截器
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns(
                        "/group/**",
                        "/recycle/**",
                        "/shortlink/**",
                        "/user/**"
                )
                .excludePathPatterns(
                        "/user/login",
                        "/user/register"
                ).order(10);
        // token刷新的拦截器
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
                .addPathPatterns("/**")
                .order(0);

        // 黑名单拦截器
        registry.addInterceptor(new BlackListInterceptor(stringRedisTemplate))
                .addPathPatterns("/**")
                .order(50);

        // 流量控制拦截器
        registry.addInterceptor(new UserFlowRiskControlInterceptor(stringRedisTemplate, userFlowRiskControlProperties))
                .addPathPatterns(
                        "/group/**",
                        "/recycle/**",
                        "/shortlink/**",
                        "/user/**"
                )
                .excludePathPatterns(
                        "/user/login",
                        "/user/register"
                ).order(100);
    }


    /**
     * 配置跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET","POST", "DELETE", "PUT")
                .allowedHeaders("*")
                .maxAge(3600);
    }

    /**
     * 接口文档
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
    @Bean
    public Docket createRestApi() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("接口文档标题")
                .version("版本信息")
                .description("接口文档描述")
                .build();

        // 文档类型
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.shortlink.controller"))
                .paths(PathSelectors.any())
                .build();
    }



}
